<?php
class BacklogsController extends AppController {

	var $name = 'Backlogs';
	var $uses = array('Task');
	var $helpers = array('Html', 'Form','Paginator');
	//var $paginate = array('limit' => 20,'order'=> array('Subscriber.firstName'=>'DESC'));

	function beforeFilter() {
		//parent::beforeFilter();
		$this->layout = 'controlpanel';
	}
	
	function index() {
		$this->Task->recursive = 1;
		$tasks = $this->paginate('Task');
		//debug($tasks);
		$this->set('tasks', $tasks);
	}
	
	function add() {
		if (!empty($this->data)) {
			$this->Task->create();
			$this->data['Task']['tasks_types_id'] = 2;
			$this->data['Task']['status_id'] = 1;
			
			$user = $this->Auth->user();
			debug($user);
			
			$this->data['Task']['created_users_id'] = $user['User']['id'];
			$this->data['Task']['projects_id'] = $this->Session->read('selectedProject');
			
			if ($this->Task->save($this->data)) {
				$this->Session->setFlash(__('The Subscriber has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The Subscriber could not be saved. Please, try again.', true));
			}
		}
		
		$relativePoints = array();
		$relativePoints['1'] = 1;
		$relativePoints['3'] = 3;
		$relativePoints['5'] = 5;
		$relativePoints['8'] = 8;
		$relativePoints['13'] = 13;
		$relativePoints['20'] = 20;
		$relativePoints['40'] = 40;
		$relativePoints['100'] = 100;
		
		$this->set(compact('relativePoints'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Subscriber', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Subscriber->save($this->data)) {
				$this->Session->setFlash(__('The Subscriber has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The Subscriber could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Subscriber->read(null, $id);
		}
		$schools = $this->Subscriber->School->find('list');
		$users = $this->Subscriber->User->find('list');
		$this->set(compact('schools','users'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Subscriber', true));
			$this->redirect(array('action' => 'index'));
		} else {
			//TODO: contact is not deleted with this code, have to change
			$this->Subscriber->rescursive = 1;
			$this->Subscriber->delete($id);
			$this->Session->setFlash(__('Subscriber deleted', true));
			$this->redirect(array('action' => 'index'));
		}
		
		$this->Session->setFlash(__('The Subscriber could not be deleted. Please, try again.', true));
		$this->redirect(array('action' => 'index'));
	}
	
	
	function search () {
		$this->paginate = array(
        	'conditions' => array('Subscriber.firstName LIKE' => '%'.$this->data['Subscriber']['subscriberKeyword'].'%')
   		);
		
		$this->set('subscribers', $this->paginate());
		$this->render('index');
	}

}
?>